<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" destroy-on-close @closed="$emit('closed')" align-center fullscreen>
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:700px;overflow:auto">
                    <el-row :gutter="1">
                        <el-col :span="6">
                            <el-form-item label="承制人">
                                <el-select v-model="form.supplier_id" clearable style="width: 100%" filterable placeholder="请选择" disabled>
                                    <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="应付">
                                <el-input v-model="info.payable" placeholder="应付" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="实付">
                                <el-input v-model="info.adtualPay" placeholder="实付" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="6">
                            <el-form-item label="补扣">
                                <el-input v-model="info.repairDeduct" placeholder="补扣" />
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="6">
                            <el-form-item label="备注">
                                <el-input v-model="form.remark" placeholder="请输入备注" />
                            </el-form-item>
                        </el-col> -->
                        <el-button :loading="isSaveing" @click="count" type="primary" style="margin-left:50px">计算补扣</el-button>
                    </el-row>
                    <el-form-item label="合同信息">
                        <sc-form-table ref="table" v-model="form.contract_data" :addTemplate="addTemplate" placeholder="暂无数据" height="300" hideAdd hideDelete>
                            <el-table-column label="批次号" prop="batch_number" align="center" width="90"></el-table-column>
                            <el-table-column label="发织合同号" prop="contract_no" align="center" width="90"></el-table-column>
                            <el-table-column label="工序名" prop="process_title" align="center"></el-table-column>
                            <el-table-column label="发织数" prop="total_weaving_count" align="center" width="90"></el-table-column>
                            <el-table-column label="收货数" prop="received_count" align="center" width="90"></el-table-column>
                            <el-table-column label="返工数" prop="rework_count" align="center" width="90"></el-table-column>
                            <el-table-column label="待收" prop="total_no_count" align="center" width="90"></el-table-column>
                            <el-table-column label="收货重" prop="total_weight" align="center" width="100"></el-table-column>
                            <el-table-column label="单价" prop="price" align="center"></el-table-column>
                            <el-table-column label="单位" prop="unit_name" align="center" width="60"></el-table-column>
                            <el-table-column label="应付" prop="total_amount" align="center" width="110"></el-table-column>
                            <el-table-column label="实付" prop="pay_money" align="center"></el-table-column>
                            <el-table-column label="货期" prop="due_date" align="center"></el-table-column>
                            <el-table-column label="末次收货" prop="take_delivery_date" align="center"></el-table-column>
                            <el-table-column label="补/扣" prop="deduction_amount" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.deduction_amount"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" prop="remark" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.remark"></el-input>
                                </template>
                            </el-table-column>
                        </sc-form-table>
                    </el-form-item>
                    <el-form-item label="毛种信息">
                        <sc-form-table ref="table" v-model="form.material_data" :addTemplate="addTemplate" placeholder="暂无数据" height="300" hideAdd hideDelete showSummary>
                            <el-table-column label="批次号" prop="batch_number" align="center"></el-table-column>
                            <el-table-column label="毛种" prop="material_name" align="center"></el-table-column>
                            <el-table-column label="损耗" prop="loss_ratio" align="center"></el-table-column>
                            <el-table-column label="预发重" prop="pre_weight" align="center"></el-table-column>
                            <el-table-column label="实发重" prop="actual_weight" align="center"></el-table-column>
                            <el-table-column label="收货重" prop="take_weight" align="center"></el-table-column>
                            <el-table-column label="退毛重" prop="return_weight" align="center"></el-table-column>
                            <el-table-column label="计毛方式" prop="way_name" align="center"></el-table-column>
                            <el-table-column label="欠毛重" prop="owe_weight" align="center"></el-table-column>
                            <el-table-column label="单价" prop="price" align="center"></el-table-column>
                            <el-table-column label="补/扣" prop="deduction_amount" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.deduction_amount"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" prop="remark" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.remark"></el-input>
                                </template>
                            </el-table-column>
                        </sc-form-table>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible=false" >取消</el-button>
                    <el-button :loading="isSaveing" @click="submit()" type="primary">保存</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '补扣款',
					edit: '修改生产任务',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
                    remark:"",
                    supplier_id:"",
                    contract_data:[],
                    material_data:[],
                    process_id:7
				},
                info:{
                    payable:"",
                    adtualPay:"",
                    repairDeduct:"",
                },
                addTemplate:{},
                applyList:[],
			}
		},
		mounted() {
            this.getApply()
        },
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.produce.send.deductionPayment.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
                this.info.payable = data.total_amount
                let a = {
                    batch_number:data.batch_number,
                    contract_no:data.contract_no,
                    process_title:data.produce_process_name,
                    price:data.price,
                    unit:data.unit,
                    unit_name:data.unit_name,
                    total_weaving_count:data.total_weaving_count,
                    received_count:data.received_count,
                    total_no_count:data.total_no_count,
                    rework_count:data.rework_count,
                    total_amount:data.total_amount,
                    pay_money:data.pay_money,
                    total_weight:data.total_weight,
                    due_date:data.due_date,
                    take_delivery_date:data.take_delivery_date,
                    remark:data.remark,
                    deduction_amount:data.deduction_amount,
                }
                let b = []
                data.contractMaterialDetail.map(item=>{
                    b.push({
                        batch_number:item.batch_number,
                        material_id:item.material_id,
                        material_name:item.material_name,
                        loss_ratio:item.loss_ratio,
                        actual_weight:item.actual_weight,
                        pre_weight:item.pre_weight,
                        // issue_weight:item.issue_weight,
                        take_weight:item.take_weight,
                        cate_id:item.cate_id,
                        return_weight:item.return_weight,
                        way_name:item.way_name,
                        owe_weight:item.owe_weight,
                        price:item.price,
                        deduction_amount:item.deduction_amount,
                        remark:item.remark,
                        material_spec_code:item.material_spec_code,
                        id:item.id,
                        total_amount:item.total_amount,
                        ratio:item.ratio
                    })
                })
                this.form.contract_data.push(a)
                this.form.material_data = b
                this.form.supplier_id = data.supplier_id
			},
            async getApply(){
                var res = await this.$API.all.Apply.get();
                this.applyList = res.data;
            },
            count(){
                let t = 0;
                let y = 0;
                for (let i = 0; i < this.form.contract_data.length; i++) {
                    t = Number(this.form.contract_data[i].deduction_amount) + t;
                }
                for (let i = 0; i < this.form.material_data.length; i++) {
                    y = Number(this.form.material_data[i].deduction_amount) + y;
                }
                this.info.repairDeduct = t + y
                this.info.adtualPay = this.info.payable - this.info.repairDeduct
            },
		}
	}
</script>

<style lang="scss"></style>
